//(TODO|FIXME|XXX|NOTE):?(\s+)?([^\r\n]+)
ko.extensions.webos.todo = function()
{
    alert("todo file");
};

ko.extensions.webos.todo.log = false;

ko.extensions.webos.todo.files = [];
ko.extensions.webos.todo.list = [];

ko.extensions.webos.todo.recursive = function(path)
{
    var file = Components.classes["@mozilla.org/file/local;1"].
                          createInstance(Components.interfaces.nsILocalFile);
    file.initWithPath(path);
    if(file.isDirectory())
    {
        var entries = file.directoryEntries;
        while(entries.hasMoreElements())
        {
            var entry = entries.getNext();
            entry.QueryInterface(Components.interfaces.nsIFile);
            ko.extensions.webos.todo.recursive(entry.path);
        }
    }
    else if(file.isFile())
    {
        ko.extensions.webos.todo.files.push(path);
    }
};

ko.extensions.webos.todo.checkFile = function(filename)
{
    var filen = Components.classes["@mozilla.org/file/local;1"].
                     createInstance(Components.interfaces.nsILocalFile);
    filen.initWithPath(ko.extensions.webos.project.path);
    filen.append("src");
    var parts = filename.split(/\/|\\/);
    for(var i in parts)
        filen.append(parts[i]);
    var fstream = Components.classes["@mozilla.org/network/file-input-stream;1"].
                            createInstance(Components.interfaces.nsIFileInputStream);
    //var cstream = Components.classes["@mozilla.org/intl/converter-input-stream;1"].
    //                        createInstance(Components.interfaces.nsIConverterInputStream);
    //fstream.init(filen, -1, 0, 0);
    
    //cstream.init(fstream, "UTF-8", 0, 0);
    //let(str = {})
    //{
    //    cstream.readString(-1, str);// read the whole file and put it in str.value
    //    content = str.value;
    //}
    //cstream.close();
    var sstream = Components.classes["@mozilla.org/scriptableinputstream;1"].
                             createInstance(Components.interfaces.nsIScriptableInputStream);
    fstream.init(filen, 0x01, 0004, 0);
    sstream.init(fstream);
    content = sstream.read(sstream.available());
    sstream.close();
    fstream.close();
    //alert(content);
    if(ko.extensions.webos.todo.log)
        ko.extensions.webos.command.appendConsole("-- Processing with ToDo...");
    var myResult = content.split(/\r?\n/);
    for(var i in myResult)
    {
        if(myResult[i].match(/([A-Z]{3,}):?(?:\s+)(\w{1,}[^\r\n]+)/) != null)
        {
            var a = myResult[i].match(/([A-Z]{3,}):?(?:\s+)(\w{1,}[^\r\n]+)/);
            if(ko.extensions.webos.todo.log)
                ko.extensions.webos.command.appendConsole("--- Line: " + (parseInt(i) + 1) + ", Type: " + a[1] + ", Content: " + a[2]);
            if(a[1] != "SDK")
            {
                ko.extensions.webos.todo.list.push({type:a[1], file:filename, line:(parseInt(i) + 1), content:a[2]});
                ko.extensions.webos.todo.add({type:a[1], file:filename, line:(parseInt(i) + 1), content:a[2]});
            }
        }
    }
    if(ko.extensions.webos.todo.log)
        ko.extensions.webos.command.appendConsole("-- Closing File...");
};

ko.extensions.webos.todo.selected = function(e)
{
    var i = document.getElementById("webosToDoItems")._lastSelectedRow;
    var line = ko.extensions.webos.todo.list[i].line;
    var relPath = ko.extensions.webos.todo.list[i].file;
    var file = Components.classes["@mozilla.org/file/local;1"].
                     createInstance(Components.interfaces.nsILocalFile);
    file.initWithPath(ko.extensions.webos.project.path);
    file.append("src");
    var parts = relPath.split(/\/|\\/);
    for(var i in parts)
        file.append(parts[i]);
    ko.views.manager.doFileOpenAtLineAsync(ko.uriparse.pathToURI(file.path),
                                           line,
                                           'editor',
                                           null,
                                           -1);
};

ko.extensions.webos.todo.add = function(o)
{
    var treerow = document.createElement("treerow");
    switch(o["type"])
    {
        case "TODO":
            treerow.setAttribute("properties", "todo_todo");
            break;
        case "BUG":
            treerow.setAttribute("properties", "todo_bug");
            break;
        case "FIXME":
        case "FIX":
            treerow.setAttribute("properties", "todo_fix");
            break;
        case "XXX":
            treerow.setAttribute("properties", "todo_xxx");
            break;
        default:
            treerow.setAttribute("properties", "todo_info");
    }
    for(var i in o)
    {
        if(i != "type")
        {
            var treecell = document.createElement("treecell");
            treecell.setAttribute("label", o[i]);
            treerow.appendChild(treecell);
        }
        else
        {
            var treecell = document.createElement("treecell");
            treecell.setAttribute("properties", "todo_icon");
            switch(o[i])
            {
                case "TODO":
                    treecell.setAttribute("label", "ToDo");
                    treerow.appendChild(treecell);
                    break;
                case "BUG":
                    treecell.setAttribute("label", "Bug");
                    treerow.appendChild(treecell);
                    break;
                case "FIXME":
                case "FIX":
                    treecell.setAttribute("label", "Fix");
                    treerow.appendChild(treecell);
                    break;
                case "XXX":
                    treecell.setAttribute("label", "xxx");
                    treerow.appendChild(treecell);
                    break;
                default:
                    treecell.setAttribute("label", o[i]);
                    treerow.appendChild(treecell);
            }
        }
    }
    var treeitem = document.createElement("treeitem");
    treeitem.appendChild(treerow);
    document.getElementById("webosToDoItems").appendChild(treeitem);
};

ko.extensions.webos.todo.clear = function()
{
    ko.extensions.webos.jslint.list = [];
    var items = document.getElementById("webosToDoItems");
    if(items.hasChildNodes())
    {
        while (items.childNodes.length >= 1)
        {
            items.removeChild(items.firstChild);
        }
    }
};

ko.extensions.webos.todo.project = function()
{
    if(ko.extensions.webos.project.isWebOS)
    {
        if(ko.extensions.webos.todo.log)
            ko.extensions.webos.command.truncateConsole("ToDo File Scan Started");
        ko.extensions.webos.todo.files = [];
        var file = Components.classes["@mozilla.org/file/local;1"].
                              createInstance(Components.interfaces.nsILocalFile);
        file.initWithPath(ko.extensions.webos.project.path);
        file.append("src");
        ko.extensions.webos.todo.recursive(file.path);
        ko.extensions.webos.todo.clear();
        var p = ko.projects.manager.currentProject;
        var files = ko.extensions.webos.todo.files;
        if(ko.extensions.webos.todo.log)
            ko.extensions.webos.command.appendConsole(ko.extensions.webos.project.path);
        for(var i in files)
        {
            var ext = /([^\.]+?)$/.exec(files[i]);
            if(ext != null)
            {
                if(ext[1] == "js")
                {
                    if(/(\/|\\)src(\/|\\)(.+)$/.exec(files[i]) != null)
                    {
                        
                        var file = /(\/|\\)src(\/|\\)(.+)$/.exec(files[i])[3];
                        if(ko.extensions.webos.todo.log)
                            ko.extensions.webos.command.appendConsole("- " + file);
                        ko.extensions.webos.todo.checkFile(file);
                    }
                }
            }
        }
        if(ko.extensions.webos.todo.log)
            ko.extensions.webos.command.appendConsole("ToDo File Scan Done");
    }
    else
    {
        alert("ToDo Only Works on webOS Projects at this Time");
    }
};